   
  function getQueryVariable(variable) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++) {
      var pair = vars[i].split("=");
      if (pair[0] == variable) {
        return pair[1];
      }
    }
    return (false);
  }

  function toUrl (url) {
         
    if (url.indexOf('ClassScheduleV2.shtml') != -1) {
      location.href = busi + '/study/index.html#/timetable/timetable'
      return
    }


    var index =  url.indexOf('#')
    if (index != -1) {
      var hash = url.split('#')[1]
      if (/^\//.test(hash)) {
      
        location.href = busi + '/study/index.html#' + hash
      } else {
     
        location.href = busi + '/study/index.html#' + '/' + hash
      }
    }else {
      location.href = url
    }
  }




  function start(){
    document.writeln("start 1");
      var code = getQueryVariable('code')
      var roleCode = getQueryVariable('roleCode')
      var access_token = getQueryVariable('access_token')
      var webUrl = getQueryVariable('webUrl')
      var classId = getQueryVariable('classId')
      var studentId = getQueryVariable('studentId')
      document.writeln("start 2");
      $.cookie('isMini', 1, {path: '/'})

      if(roleCode){
        $.cookie('roleCode',roleCode, {expires: 100, path: '/', domain: 'xueerqin.net'})
      }
      if(webUrl){
        $.cookie('webUrl',decodeURIComponent(webUrl), {expires: 100, path: '/', domain: 'xueerqin.net'})
      }
      if(classId){
        $.cookie('classId',classId, {expires: 100, path: '/', domain: 'xueerqin.net'})
      }
  
      if(studentId){
        $.cookie('studentId', studentId, {expires: 100, path: '/', domain: 'xueerqin.net'})
      }
      document.writeln("start 3");
      if(access_token){
        $.cookie('authToken', JSON.stringify({
            access_token: access_token,
            expires_in: 3600,
            token_type: 'Bearer'
        }), {
            expires: 100,
            path: '/',
            domain: 'xueerqin.net'
        })
      }
      
      document.writeln("start 4");

        if(code){
            var first = $.cookie('first')
            document.writeln("have code");
            if(first){
              document.writeln("have first = " + first);
                $.cookie('first', '', {
                  expires: 100,
                  path: '/',
                  domain: 'xueerqin.net'
              })

                wx.miniProgram.navigateBack()
                return
            } else {
              document.writeln("no first");
                $.cookie('first','1', {
                  expires: 100,
                  path: '/',
                  domain: 'xueerqin.net'
              })
            }
        } else {
            document.writeln("no code");
            $.cookie('first', '', {
              expires: 100,
              path: '/',
              domain: 'xueerqin.net'
          })
        }
        

        userWechatInfoForComponent().then(res => {
            
            openid = res.openId
            unionid = res.unionId
        
            
            return miniProgramLogin({
            openid: openid,
            unionid: unionid
            })
        
        }).then((res) => {
            
            $.cookie('authToken', JSON.stringify(res.oAuth), {
              path: '/',
              domain: '.xueerqin.net',
              expires: 100
            })

            $.cookie("loginName", res.loginName, { expires: 100, path: '/', domain: 'xueerqin.net' });
            $.cookie("cellPhoneNum", res.loginName, { expires: 100, path: '/', domain: 'xueerqin.net' });

            // $.cookie('isMoreRole', false, {expires: 100, path: '/', domain: 'xueerqin.net'})
            var roleCode = $.cookie('roleCode')
            var classId = $.cookie('classId')
            var studentId = $.cookie('studentId')
            if(roleCode == 'Teacher'){
              localStorage.setItem('itemDataTeacher', JSON.stringify({
                ClassId: classId
              }))
            } else if(roleCode == 'Parent'){
              localStorage.setItem('itemData', JSON.stringify({
                SelfId: studentId
              }))
            }
            
            toUrl($.cookie('webUrl'))
        })
  
  }





(function(){
  try {
    start()
  } catch (error) {
    document.writeln(JSON.stringify(error))
  }
   
})()
  
  